/**
 * The contents of this file are subject to the license and copyright detailed in the LICENSE and NOTICE files at the root of the source tree and available online at
 * http://www.askosi.org/license.html
 */
package be.destin.skos.rdf;

import java.util.HashMap;

import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.RDFHandlerException;

import be.destin.skos.core.Concept;
import be.destin.skos.core.ConceptScheme;
import be.destin.skos.core.SchemeInterface;
import be.destin.skos.core.SkosManager;

public class SkosRdfHandler implements RDFHandler {

	public static final String rdfNamespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
	public static final String rdfsNamespace = "http://www.w3.org/2000/01/rdf-schema#";
	public static final String dcNamespace = "http://purl.org/dc/elements/1.1/";
	public static final String dctermsNamespace = "http://purl.org/dc/terms/";
	public static final String skosNamespace = "http://www.w3.org/2004/02/skos/core#";

	final static URI rdf_Type = new URIImpl(rdfNamespace+"Type");
	final static URI skos_ConceptScheme = new URIImpl(skosNamespace+"ConceptScheme");
	final static URI skos_Concept = new URIImpl(skosNamespace+"Concept");
	
	HashMap<URI,SchemeInterface> uriScheme = new HashMap<URI,SchemeInterface>();
	HashMap<URI,URI>subjectType;//???

	SkosManager skosManager = null;

	public void addScheme(URI subject, String aboutScheme) {
		SchemeInterface aScheme = skosManager.getScheme(subject.getLocalName());
		if (aScheme == null) {
			aScheme = new ConceptScheme();
		}
		uriScheme.put(subject, aScheme);
	}

	// Handles a comment.
	public void handleComment(String arg0) throws RDFHandlerException {
	}

	// Handles a namespace declaration/definition.
	public void handleNamespace(String prefix, String namespace)
			throws RDFHandlerException {
	}

	// Handles a statement.
	public void handleStatement(Statement rdfStatement) throws RDFHandlerException {
		Resource subject = rdfStatement.getSubject();
		URI predicate = rdfStatement.getPredicate();
		Value object = rdfStatement.getObject();
		/*
		String content = "";
		String lang = "";
		if (object instanceof Literal) {
			content = ((Literal)object).getLabel();
			lang = ((Literal)object).getLanguage();
		} else if (object instanceof URI) {
			content = ((URI)subject).stringValue();
		}
		*/ 
		if (predicate.getNamespace().equals(rdfNamespace)
				&& predicate.getLocalName().equals("Type")) {
			subjectType.put(((URI)subject),(URI) object);
		} else {
			URI type = subjectType.get(((URI)subject));
			if (type.getNamespace().equals(skosNamespace)) {
				if (type.getLocalName().equals(Concept.class.getSimpleName())) {
					Concept aConcept = skosManager.getScheme_Concept("????_"+((URI)subject).getLocalName());					
				} else if (type.getLocalName().equals(ConceptScheme.class.getSimpleName())) {
					ConceptScheme aScheme = skosManager.getConceptScheme(((URI)subject).getLocalName());
				}
			}
		}
		// Resource context = rdfStatement.getContext();
	}

	// Signals the start of the RDF data.
	public void startRDF() throws RDFHandlerException {
		subjectType = new HashMap<URI,URI>();
	}

	// Signals the end of the RDF data.
	public void endRDF() throws RDFHandlerException {
		subjectType = null;
	}

	public SkosManager getSkosManager() {
		return skosManager;
	}

	public void setSkosManager(SkosManager skosManager) {
		this.skosManager = skosManager;
	}

}
